<template>
  <div>
    <div class="form-header">
      <div class="title">案件检索</div>
      <el-button @click="handleCacel">取消</el-button>
    </div>
    <div class="page">
      <DynamicForm
        ref="queryRef"
        v-model="queryParams"
        :disabled="true"
        :config="formConfig"
        :items="formItems"
        :dict-data="{ case_source }" />

      <!-- 时间轴附件区域 -->
      <div class="timeline-container" v-if="queryParams.docList.length">
        <el-timeline>
          <el-timeline-item
            v-for="(item, index) in queryParams.docList"
            :key="index"
            :timestamp="item.timestamp"
            placement="top"
            :color="item.color || '#0bbd87'">
            <el-card shadow="hover">
              <div class="timeline-header">
                <div>
                  <span style="font-size: 16px; font-weight: 600">{{ item.name }}</span>
                  <span style="margin-left: 16px">{{ item.updateTime }}</span>
                </div>
                <div class="timeline-actions">
                  <el-button link type="primary" @click="handleViewAll(item)">查看</el-button>
                  <el-button :loading="item.exportLoading" link type="primary" @click="handleExport(item)">
                    导出
                  </el-button>
                  <el-button :loading="item.preViewLoading" link type="primary" @click="handlePreview(item)">
                    预览
                  </el-button>
                </div>
              </div>
              <div class="timeline-content">
                <FileUpload
                  v-if="item?.files?.length"
                  v-model="item.files"
                  :isUrl="true"
                  :disabled="true"
                  :is-show-tip="false"
                  :showBtn="false" />
                <div v-else>暂无附件</div>
              </div>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { pageConfig } from './config'
import { getRegistrationCase } from '@/api/caseSearch'
import FileUpload from '@/components/FileUpload/index.vue'
import { onSiteInspectionExport } from '@/api/onSiteInspection/index.js'
import { downloadBlob } from '@/utils/download.js'
import { onSiteInspectionPreviewList, onSiteInspectionPreview } from '@/api/system/file.js'
import { exportFilingApprovalForm, registrationcasePreview } from '@/api/filingReview/register.js'
import { previewPenaltydecisions, exportPenaltydecisions } from '@/api/caseHandling/administrativePenalty'
import { previewPenaltydeferralapply, exportPenaltydeferralapply } from '@/api/penalty/delayApproval'
import { exportPenaltydeferralresult, previewPenaltydeferralresult } from '@/api/decisionl/index.js'
import { previewNoticedemandapproval, exportNoticedemandapproval } from '@/api/reminderLetterApp/index.js'
import {
  previewNoticedemandapproval as previewNoticedemandapproval2,
  exportNoticedemandapproval as exportNoticedemandapproval2,
} from '@/api/reminderLetter/index.js'
import {
  previewNoticedemandapproval as previewNoticedemandapproval3,
  exportNoticedemandapproval as exportNoticedemandapproval3,
} from '@/api/compulsoryApp/index.js'
import { previewCompulsoryItemApprovalResult, exportCompulsoryItemApprovalResult } from '@/api/enforcement/index.js'
import { previewInquiry, exportPenaltydecisions as exportPenaltydecisions2 } from '@/api/inquiry/index'
import { previewcompulsoryrectify, exportcompulsoryrectify } from '@/api/orderRectification/index.js'
import { previewSiterecheck, exportSiterecheck } from '@/api/onSiteReview/index.js'
import { previewPenaltyAdvanceNotice, exportPenaltyAdvanceNotice } from '@/api/priorNotice/approval.js'
import { previewPenalty, exportPenalty } from '@/api/priorNotice/notice.js'
import { previewCaseProcessApproval, exportCaseProcessApproval } from '@/api/caseHandling/approval.js'
import { previewLegalReviewOpinion, exportLegalReviewOpinion } from '@/api/caseHandling/legalReview.js'
import { previewGroupDiscussion, exportGroupDiscussion } from '@/api/caseHandling/groupDiscussion.js'
import { previewHearingNoticeApproval, exportHearingNoticeApproval } from '@/api/hearingNotice/approval.js'
import { previewHearingNotice, exportHearingNotice } from '@/api/hearingNotice/notice.js'
import { previewNotification, exportNotification } from '@/api/hearingNotice/notification.js'
import { previewHearingRequest, exportHearingRequest } from '@/api/hearingNotice/record.js'
import { previewHearingReport, exportHearingReport } from '@/api/hearingNotice/report.js'
import { previewCaseclosureapproval, exportCaseclosureapproval } from '@/api/caseClosed/index.js'
import { previewCasecancelapproval, exportCasecancelapproval } from '@/api/caseWithdrawal/index.js'

const route = useRoute()
const router = useRouter()

const { proxy } = getCurrentInstance() as any
const { case_source } = proxy.useDict('case_source')

const queryRef = ref()
const queryParams = ref({
  docList: [],
})
const disabled = route.query.type === 'view'
const { formConfigDetail: formConfig, formItemsDetail: formItems } = pageConfig()

const getFileDetail = async id => {
  const res = await getRegistrationCase(id)
  queryParams.value = { ...res.data.registrationCase, docList: res.data.docList || [] }
}

// 取消
const handleCacel = () => {
  router.back()
}

// 查看
const handleViewAll = item => {
  // @ts-ignore
  const baseUrl = import.meta.env.VITE_APP_BASE || ''
  let url = ''
  switch (item.type) {
    case 1:
    case 2:
      // 现场检查 + 现场检查照片
      url = `${baseUrl}/workbench/filingReview/onSiteInspection?formId=${item.formId}`
      break
    case 3:
      // 立案审批表
      url = `${baseUrl}workbench/filingReview/registerDetail?id=${item.formId}&type=view`
      break
    case 4:
      // 行政处罚决定书
      url = `${baseUrl}/workbench/caseHandling/administrativePenalty?formId=${item.formId}`
      break
    case 6:
      // 分期（延期）缴纳罚款审批表
      url = `${baseUrl}/workbench/penalty/delayApproval?formId=${item.formId}`
      break
    case 7:
    case 8:
      // 同意/不予分期（延期）处罚决定书
      url = `${baseUrl}/workbench/penalty/decisionl?formId=${item.formId}`
      break
    case 10:
      // 崔告书审批表
      url = `${baseUrl}/workbench/penalty/reminderLetterApp?formId=${item.formId}`
      break
    case 11:
      // 行政处罚决定书履行催告书
      url = `${baseUrl}/workbench/penalty/reminderLetter?formId=${item.formId}`
      break
    case 13:
      // 行政强制审批表
      url = `${baseUrl}/workbench/penalty/compulsoryApp?formId=${item.formId}`
      break
    case 14:
      // 强制执行申请书
      url = `${baseUrl}/workbench/penalty/enforcement?formId=${item.formId}`
      break
    case 15:
      // 调查询问
      url = `${baseUrl}/workbench/inquiryDetail?formId=${item.formId}&type=view`
      break
    case 16:
      // 责令整改
      url = `${baseUrl}/workbench/filingReview/orderRectification?formId=${item.formId}`
      break
    case 17:
    case 18:
      // 现场复核/照片
      url = `${baseUrl}workbench/filingReview/onSiteReview?formId=${item.formId}`
      break
    case 19:
      // 事先告知审批表
      url = `${baseUrl}/workbench/priorNotice/approval?formId=${item.formId}`
      break
    case 20:
    case 21:
      // 行政/不予处罚事先告知书
      url = `${baseUrl}/workbench/priorNotice/notice?formId=${item.formId}`
      break
    case 22:
      // 案件处理审批表
      url = `${baseUrl}/workbench/caseHandling/approval?formId=${item.formId}`
      break
    case 23:
      // 法制审核意见书
      url = `${baseUrl}/workbench/caseHandling/legalReview?formId=${item.formId}`
      break
    case 24:
      // 集体讨论
      url = `${baseUrl}/workbench/caseHandling/groupDiscussion?formId=${item.formId}`
      break
    case 25:
      // 听证告知审批表
      url = `${baseUrl}/workbench/hearingNotice/approval?formId=${item.formId}`
      break
    case 26:
      // 行政处罚听证告知书
      url = `${baseUrl}/workbench/hearingNotice/notice?formId=${item.formId}`
      break
    case 27:
      // 行政处罚听证通知书
      url = `${baseUrl}/workbench/hearingNotice/notification?formId=${item.formId}`
      break
    case 28:
      // 听证笔录
      url = `${baseUrl}/workbench/hearingNotice/record?formId=${item.formId}`
      break
    case 29:
      // 听证报告
      url = `${baseUrl}/workbench/hearingNotice/report?formId=${item.formId}`
      break
    case 40:
      // 结案
      url = `${baseUrl}/workbench/caseclosure/caseClosed?formId=${item.formId}`
      break
    case 41:
      // 撤案申请
      url = `${baseUrl}/workbench/caseclosure/caseWithdrawalIndex?formId=${item.formId}`
      break
    default:
      break
  }
  if (url) {
    window.open(url, '_blank')
  }
}
// 导出
const handleExport = async item => {
  let exportApi: any = null
  item.exportLoading = true
  switch (item.type) {
    case 1:
    case 2:
      // 现场检查 + 现场检查照片
      exportApi = onSiteInspectionExport
      break
    case 3:
      // 立案审批表
      exportApi = exportFilingApprovalForm
      break
    case 4:
      // 行政处罚决定书
      exportApi = exportPenaltydecisions
      break
    case 6:
      // 分期（延期）缴纳罚款审批表
      exportApi = exportPenaltydeferralapply
      break
    case 7:
    case 8:
      // 同意/不予分期（延期）处罚决定书
      exportApi = exportPenaltydeferralresult
      break
    case 10:
      // 崔告书审批表
      exportApi = exportNoticedemandapproval
      break
    case 11:
      // 行政处罚决定书履行催告书
      exportApi = exportNoticedemandapproval2
      break
    case 13:
      // 行政强制审批表
      exportApi = exportNoticedemandapproval3
      break
    case 14:
      // 强制执行申请书
      exportApi = exportCompulsoryItemApprovalResult
      break
    case 15:
      // 调查询问
      exportApi = exportPenaltydecisions2
      break
    case 16:
      // 责令整改
      exportApi = exportcompulsoryrectify
      break
    case 17:
    case 18:
      // 现场复核/照片
      exportApi = exportSiterecheck
      break
    case 19:
      // 事先告知审批表
      exportApi = exportPenaltyAdvanceNotice
      break
    case 20:
    case 21:
      // 行政/不予处罚事先告知书
      exportApi = exportPenalty
      break
    case 22:
      // 案件处理审批表
      exportApi = exportCaseProcessApproval
      break
    case 23:
      // 法制审核意见书
      exportApi = exportLegalReviewOpinion
      break
    case 24:
      // 集体讨论
      exportApi = exportGroupDiscussion
      break
    case 25:
      // 听证告知审批表
      exportApi = exportHearingNoticeApproval
      break
    case 26:
      // 行政处罚听证告知书
      exportApi = exportHearingNotice
      break
    case 27:
      // 行政处罚听证通知书
      exportApi = exportNotification
      break
    case 28:
      // 听证笔录
      exportApi = exportHearingRequest
      break
    case 29:
      // 听证报告
      exportApi = exportHearingReport
      break
    case 40:
      // 结案
      exportApi = exportCaseclosureapproval
      break
    case 41:
      // 撤案申请
      exportApi = exportCasecancelapproval
      break
    default:
      break
  }

  const res = await exportApi([item.formId])
  downloadBlob(res)
  item.exportLoading = false
}
// 预览
const handlePreview = async item => {
  let preViewApi: any = null
  item.preViewLoading = true
  switch (item.type) {
    case 1:
    case 2:
      // 现场检查 + 现场检查照片
      preViewApi = onSiteInspectionPreview
      break
    case 3:
      // 立案审批表
      preViewApi = registrationcasePreview
      break
    case 4:
      // 行政处罚决定书
      preViewApi = previewPenaltydecisions
      break
    case 6:
      // 分期（延期）缴纳罚款审批表
      preViewApi = previewPenaltydeferralapply
      break
    case 7:
    case 8:
      // 同意/不予分期（延期）处罚决定书
      preViewApi = previewPenaltydeferralresult
      break
    case 10:
      // 崔告书审批表
      preViewApi = previewNoticedemandapproval
      break
    case 11:
      // 行政处罚决定书履行催告书
      preViewApi = previewNoticedemandapproval2
      break
    case 13:
      // 行政强制审批表
      preViewApi = previewNoticedemandapproval3
      break
    case 14:
      // 强制执行申请书
      preViewApi = previewCompulsoryItemApprovalResult
      break
    case 15:
      // 调查询问
      preViewApi = previewInquiry
      break
    case 16:
      // 责令整改
      preViewApi = previewcompulsoryrectify
      break
    case 17:
    case 18:
      // 现场复核/照片
      preViewApi = previewSiterecheck
      break
    case 19:
      // 事先告知审批表
      preViewApi = previewPenaltyAdvanceNotice
      break
    case 20:
    case 21:
      // 行政/不予处罚事先告知书
      preViewApi = previewPenalty
      break
    case 22:
      // 案件处理审批表
      preViewApi = previewCaseProcessApproval
      break
    case 23:
      // 法制审核意见书
      preViewApi = previewLegalReviewOpinion
      break
    case 24:
      // 集体讨论
      preViewApi = previewGroupDiscussion
      break
    case 25:
      // 听证告知审批表
      preViewApi = previewHearingNoticeApproval
      break
    case 26:
      // 行政处罚听证告知书
      preViewApi = previewHearingNotice
      break
    case 27:
      // 行政处罚听证通知书
      preViewApi = previewNotification
      break
    case 28:
      // 听证笔录
      preViewApi = previewHearingRequest
      break
    case 29:
      // 听证报告
      preViewApi = previewHearingReport
      break
    case 40:
      // 结案
      preViewApi = previewCaseclosureapproval
      break
    case 41:
      // 撤案申请
      preViewApi = previewCasecancelapproval
      break
    default:
      break
  }
  if ([1, 2].includes(item.type)) {
    const res1 = await onSiteInspectionPreviewList({ id: item.formId })
    const res = await preViewApi({
      type: item.type,
      id: res1.data.find(itemx => Number(itemx.type) === item.type)?.id,
    })
    window.open(res.msg)
    item.preViewLoading = false
    return
  }
  if ([17, 18].includes(item.type)) {
    const res = await preViewApi({
      type: item.type === 17 ? '1' : '2',
      id: item.formId,
    })
    window.open(res.msg)
    item.preViewLoading = false
    return
  }
  if ([10, 11, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 40, 41].includes(item.type)) {
    const res = await preViewApi({ id: item.formId })
    window.open(res.msg)
    item.preViewLoading = false
    return
  }
  const res = await preViewApi(item.formId)
  window.open(res.msg)
  item.preViewLoading = false
}

onMounted(() => {
  if (route.query.id) {
    getFileDetail(route.query.id)
  }
})
</script>

<style scoped lang="scss">
.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .title {
    color: var(--el-text-color-primary);
    font-size: var(--el-dialog-title-font-size);
    line-height: var(--el-dialog-font-line-height);
  }
}
.timeline-container {
  margin-top: 30px;
  padding: 20px;
  background: #fff;
  border-radius: 4px;
}

.timeline-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;

  h4 {
    margin: 0;
    font-size: 16px;
    font-weight: 500;
  }

  .timeline-actions {
    display: flex;
    gap: 5px;
  }
}

.timeline-content {
  margin-top: 10px;
}

:deep(.el-timeline-item__timestamp) {
  color: #909399;
  font-size: 13px;
}

:deep(.el-timeline-item__wrapper) {
  padding-left: 20px;
}

:deep(.el-card) {
  border: 1px solid #ebeef5;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
}

:deep(.el-card__body) {
  padding: 15px;
}
</style>
